home *** CD-ROM | disk | FTP | other *** search
- /* @(#) $Header: netlog.c,v 1.4 91/04/25 18:27:17 deyke Exp $ */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/rtprio.h>
- #include <unistd.h>
-
- #include "global.h"
-
- #define LOGFILEDIR "/tcp/logs"
- #define START_REC 1
-
- static FILE *fplog;
- static FILE *fps[_NFILE];
- static int lastchr[_NFILE];
-
- static void netlog __ARGS((void));
- static void start_netlog __ARGS((void));
-
- /*---------------------------------------------------------------------------*/
-
- static void netlog()
- {
-
- char filename[80];
- int chr;
- int i;
- int s;
- static int cnt;
-
- for (; ; ) {
- while (getc(fplog) != START_REC)
- if (ferror(fplog) || feof(fplog)) return;
- i = getc(fplog);
- if (i < 0 || i >= _NFILE) continue;
- s = getw(fplog);
- if (s > 0) {
- if (!fps[i]) {
- sprintf(filename, "%s/log.%05d.%04d", LOGFILEDIR, getpid(), cnt++);
- fps[i] = fopen(filename, "a");
- }
- if (!fps[i]) {
- while (s--) chr = getc(fplog);
- } else {
- while (s--) {
- chr = getc(fplog);
- if (!chr) {
- /* ignore nulls */
- } else if (chr == '\r')
- putc('\n', fps[i]);
- else if (chr != '\n' || lastchr[i] != '\r')
- putc(chr, fps[i]);
- lastchr[i] = chr;
- }
- fflush(fps[i]);
- }
- } else if (s < 0 && fps[i]) {
- fclose(fps[i]);
- fps[i] = 0;
- lastchr[i] = 0;
- }
- }
- }
-
- /*---------------------------------------------------------------------------*/
-
- static void start_netlog()
- {
- int fd[2], i;
-
- if (pipe(fd)) return;
- switch (fork()) {
- case -1:
- close(fd[0]);
- close(fd[1]);
- break;
- case 0:
- rtprio(0, RTPRIO_RTOFF);
- for (i = 0; i < _NFILE; i++)
- if (i != fd[0]) close(i);
- fplog = fdopen(fd[0], "r");
- netlog();
- exit(0);
- break;
- default:
- close(fd[0]);
- fplog = fdopen(fd[1], "w");
- break;
- }
- }
-
- /*---------------------------------------------------------------------------*/
-
- void write_log(fd, buf, cnt)
- int fd;
- char *buf;
- int cnt;
- {
- if (!cnt) return;
- if (!fplog) {
- start_netlog();
- if (!fplog) return;
- }
- putc(START_REC, fplog);
- putc(fd, fplog);
- putw(cnt, fplog);
- while (--cnt >= 0) putc(*buf++, fplog);
- fflush(fplog);
- }
-
-